import QtQuick 1.1
import com.nokia.meego 1.0
import com.meego.extras 1.0 // Need to uncomment it to deploy

import "validate.js" as Validate

Page {
    id:loginPage

    orientationLock: PageOrientation.LockPortrait

    HeaderBar {
        id: top_banner
        havemenu:false
        havecurrentdir:false
        title: "DropN9"
    }

    Rectangle {
        id: r_title
        y: top_banner.height
        width: parent.width
        height: 38
        color: "transparent"

        Label {
            id: t_title
            color: "#185264"
            text: qsTr("Dropbox client for N9")
            horizontalAlignment: Text.AlignHCenter
            font.bold: true
            font.pixelSize: 25
            anchors.centerIn: parent
        }
    }

    // Need to uncomment them to deploy
    InfoBanner {
        id: i_errorbanner
        timerEnabled: true
        timerShowTime: 3000
    }


    Rectangle {
        id: r_break_line
        x: -1
        y: r_title.y + r_title.height + 3
        width: parent.width + 1
        color:"black"
        border.color: "grey"
        height: 1
    }

    Rectangle {
        id: r_login_zone
        y: r_title.y + r_title.height + 20
        width: parent.width
        color: "transparent"

        Row {
            id: r_login_email_zone
            anchors.horizontalCenter: parent.horizontalCenter

            Row {
                Text {
                    id: l_email
                    text: "Email"
                    color: "#373c3b"
                    width:parent.width
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: 18
                    font.family: "Nokia Pure Text Light"
                    height: 40
                }
            }

            Row {
                TextField {
                    id:t_email
                    y: l_email.height - 10
                    font.pixelSize: 20
                    width: 460
                    font.family: "Nokia Pure Text Light"
                    text:controllerMIT.opt_user_email()
                }
            }
        }// end row email

        Row {
            id: r_login_password_zone
            y: r_login_email_zone.height + 30
            anchors.horizontalCenter: parent.horizontalCenter

            Row {
                Text {
                    id: l_password
                    text: "Password"
                    font.family: "Nokia Pure Text Light"
                    color: "#373c3b"
                    width:parent.width
                    height: 40
                    horizontalAlignment: Text.AlignRight
                    verticalAlignment: Text.AlignVCenter
                    font.pixelSize: 18
                }
            }

            Row {
                TextField {
                    id:t_password
                    y: l_password.height - 10
                    echoMode: TextInput.Password
                    font.pixelSize: 20
                    width: 460
                    font.family: "Nokia Pure Text Light"
                    text:controllerMIT.opt_user_password()
                }
            }
        }// end row password

        Row {
            id: r_login_signin_button
            y: r_login_email_zone.height + r_login_password_zone.height + 80

            Column {
                width:loginPage.width

                Button {
                    id: btn_login
                    font.family: "Nokia Pure Text Light"
                    text:"Sign in"
                    platformStyle: ButtonStyle{inverted:true}
                    width: 270
                    anchors.centerIn: parent

                    onClicked: {
                        if (!Validate.is_not_empty(t_email.text)){
                            t_email.focus = true
                            i_errorbanner.text = "Please enter email."
                            i_errorbanner.show()
                        }else if(!Validate.is_email(t_email.text)){
                            t_email.focus = true
                            i_errorbanner.text = "Please enter a valid email."
                            i_errorbanner.show()
                        }else if(!Validate.is_not_empty(t_password.text)){
                            t_password.focus = true
                            i_errorbanner.text = "Please enter password."
                            i_errorbanner.show()
                        }else{
                            controllerMIT.authenticate(t_email.text, t_password.text)
                            waitingSheet.open()
                        }

                    }
                }
            }
        }// end row r_login_signin_button button

    }// end rectangle r_login_zone

    Sheet {
        id:waitingSheet
        rejectButtonText: "Cancel"
        content: Flickable {
            anchors.fill: parent
            BusyIndicator {
                id:b_indicator
                anchors.centerIn: parent
                running: true
                platformStyle: BusyIndicatorStyle { size: "large" }
            }
        }
        onRejected: controllerMIT.cancel_login()
    }

    Rectangle {
        id: r_suggest_to_create_new_account
        y: parent.height - 80
        width: parent.width
        color: "transparent"

        Text{
            id: t_question
            text:"Don't have an account yet ?"
            width:parent.width
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: 20
            font.family: "Nokia Pure Text Light"
        }

        Label{
            text:"<a href='#'>Create account here</a>"
            y: t_question.height + 5
            width:parent.width
            horizontalAlignment: Text.AlignHCenter
            verticalAlignment: Text.AlignVCenter
            font.pixelSize: 22
            font.bold: true
            font.family: "Nokia Pure Text Light"

            MouseArea {
                anchors.fill: parent
                onClicked: Qt.openUrlExternally("https://www.dropbox.com/register")
            }
        }
        z:1
    } // end r_suggest_to_create_new_account

    Connections {
        target: controllerMIT

        onNetwork_error : {
            waitingSheet.close()
            i_errorbanner.text = error
            i_errorbanner.show()
        }

        onAuthenticate_finished : {
            waitingSheet.close()
            pageStack.pop()
            pageStack.push(Qt.resolvedUrl("FolderList.qml"))
        }
    }

    tools: ToolBarLayout {
        visible: true
    }

}
